/*
 * @Author: Lee.mark 
 * @Date: 2018-05-06 20:53:05 
 * @Last Modified by: Lee.mark
 * @Last Modified time: 2018-05-06 20:53:25
 */
$(function () { /* 文档加载，执行一个函数*/
  $('#register_form')
    .bootstrapValidator({
      feedbackIcons: { /*input状态样式图片*/
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
      },
      fields: { /*验证：规则*/
        email: {
          validators: {
            notEmpty: {
              message: '邮件不能为空'
            },
            emailAddress: {
              message: '请输入正确的邮件地址如：123@qq.com'
            }
          }
        },
        // 昵称
        nickname: { //验证input项：验证规则
          validators: {
            notEmpty: {
              message: '昵称不能为空'
            },
            stringLength: {
              min: 4,
              max: 30,
              message: '昵称的长度必须在4到30之间'
            }
          }
        },
        password: {
          validators: {
            notEmpty: {
              message: '密码不能为空'
            },
            stringLength: {
              min: 6,
              max: 30,
              message: '密码长度必须在6到30之间'
            },
            different: { //不能和用户名相同
              field: 'username', //需要进行比较的input name值
              message: '不能和用户名相同'
            }
          }
        }
      }
    })
    .on('success.form.bv', function (e) { //点击提交之后
      // Prevent form submission
      e.preventDefault();

      // Get the form instance
      var $form = $(e.target);

      // Get the BootstrapValidator instance
      var bv = $form.data('bootstrapValidator');

      // Use Ajax to submit form data 提交至form标签中的action，result自定义
      $.ajax({
        type: 'post',
        url: '/signup',
        data: $form.serialize(),
        success: function (data) {
          // console.log(data);
          if (data.code == 1002) {
            location.href = '/'
          }
        }
      });
    });
});